package com.burningtower.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.burningtower.common.PageResult;
import com.burningtower.common.Result;
import com.burningtower.entity.User;
import com.burningtower.service.IUserService;
import com.burningtower.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author zhe
 * @since 2017-11-17
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService service;

    @RequestMapping("dataGrid")
    public PageResult<UserVo> dataGrid(@RequestParam(required = false)String name,
                                       @RequestParam(required = false)Date createdateStart,
                                       @RequestParam(required = false)Date createdateEnd,
                                       @RequestParam(required = false)Integer organizationId,
                                       @RequestParam(required = false,defaultValue = "asc") String order,
                                       @RequestParam(required = false,defaultValue = "1") Integer page,
                                       @RequestParam(required = false,defaultValue = "20") Integer rows,
                                       @RequestParam(required = false,defaultValue = "id") String sort){
        sort=StringUtils.camelToUnderline(sort);
        Page<User> page1=new Page(page,rows,sort);
        page1.setAsc("asc".equals(order));
        Page<User> page2;
        if(name!=null&&!"".equals(name)||createdateStart!=null||createdateEnd!=null){
            EntityWrapper<User> entityWrapper = new EntityWrapper<>();
            if(name!=null&&!"".equals(name)){
                User user=new User();
                user.setName(name);
                entityWrapper.setEntity(user);
            }
            if(createdateStart!=null){
                if(createdateEnd!=null){
                    entityWrapper.between("create_time",createdateStart,createdateEnd);
                }else{
                    entityWrapper.gt("create_time",createdateStart);
                }
            }else if(createdateEnd!=null){
                entityWrapper.lt("create_time",createdateEnd);
            }
            page2 = service.selectPage(page1,entityWrapper);
        }else if(organizationId!=null){
            User user=new User();
            user.setOrganizationId(organizationId);
            page2=service.selectPage(page1,new EntityWrapper<>(user));
        }else {
            page2 = service.selectPage(page1);
        }
        List<User> records = page2.getRecords();
        List<UserVo> vos=new ArrayList<>();
        for(User user:records){
            vos.add(service.toVo(user));
        }
        return new PageResult<>(page2.getTotal(),vos);
    }
    @RequestMapping("delete")
    public Result delete(Long id){
        service.deleteById(id);
        return new Result("删除成功！");
    }
    @RequestMapping("edit")
    public Result edit(User user){
        service.updateById(user);
        return new Result("编辑成功！");
    }
    @RequestMapping("add")
    public Result add(User user){
        user.setCreateTime(new Date());
        user.setSalt("test");
        service.insert(user);
        return new Result("添加成功");
    }
}

